<?php
namespace app\Tools\Wxchat\WeChatReply;

use app\Tools\Wxchat\WeChatReply\Common;

class WechatUpload extends Common
{

    /**
     * 上传微信永久素材
     * 请注意该方法必须保证php的版本在5.6以下，否则会爆40015错误
     */
    function upload_meterial($file_info,$access_token){
        $url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";
        $ch1 = curl_init ();
        $timeout = 5;
        $real_path="{$file_info['filename']}";
        //$real_path=str_replace("/", "\\", $real_path);
        $data= array("media"=>"@{$real_path}",'form-data'=>$file_info);
        curl_setopt ( $ch1, CURLOPT_URL, $url );
        curl_setopt ( $ch1, CURLOPT_POST, 1 );
        curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data );
        $result = curl_exec ( $ch1 );
        echo '<br/>';
        echo 'reulst is ==========>'.$result;
        curl_close ( $ch1 );
        if(curl_errno()==0){
            $result=json_decode($result,true);
            //var_dump($result);
            return $result['media_id'];
        }else {
            return false;
        }
    }
}